package operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration  {
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入你要删除图书的名字：");
        Scanner sc=new Scanner(System.in);
        String name=sc.nextLine();
        int index=0;
        int current=bookList.getUsedSize();
        int i=0;
        for (;i<current;i++){
            Book book= bookList.getPos(i);//getpos是一个book类
            if(book.getName().equals(name)){
                System.out.println("找到这本书了");
                index=i;
                break;
            }
        }
        //1，遇到break 2，没有找到结束了for循环
        if(i==current){
            System.out.println("没有你要删除的图书！");
            return;
        }
         for(int j=index;j<current-1;j++) {
             Book book = bookList.getPos(j + 1);
             bookList.setBooks(j, book);
         }
         bookList.setBooks(current-1,null);
         //删完之后要置空
         bookList.setUsedSize(current-1);
         //删完之后也要让长度减1.
         }
    }

